package com.guqj.mq.rabbitmq;

import java.io.IOException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;

public class RabbitServer {
	
	
	public static void main(String[] args) throws Exception {

		directExchange();

	}
	
	
	public static void directExchange() throws Exception {
		Connection conn = null;
		Channel channel = null;
		try {
			// 建立连接
			ConnectionFactory factory = new ConnectionFactory();
			factory.setHost("localhost");
			factory.setPort(5672);
			factory.setUsername("guest");
			factory.setPassword("guest");
			//factory.setVirtualHost("test_host");
			conn = factory.newConnection();
			//channel = 
			channel = RabbitUtil.createChannel(conn);
			if (conn != null && channel != null) {
				byte[] messageBodyBytes = "Hello, world!".getBytes();
				// 绑定路由键
				channel.basicPublish(RabbitConfig.EXCHANGENAME, RabbitConfig.ROUTEINGKEY,
						MessageProperties.PERSISTENT_TEXT_PLAIN, messageBodyBytes);
				System.out.println("send message[Hello, world!] from " + RabbitConfig.QUEUENAME);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			RabbitUtil.close(conn, channel);
		}
	}
}
